package com.iv.durian.cloud.service;

import com.iv.durian.cloud.*;
import com.iv.durian.cloud.dao.LessonDao;
import com.iv.durian.cloud.dao.LessonDetailDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.sql.Time;
import java.util.Date;

/**
 * Created by zlw on 2018-07-01.
 */
@Slf4j
@Service
public class LessonDetailService {

    @Autowired
    private LessonDetailDao lessonDetailDao;

    @Autowired
    private LessonDao lessonDao;

    /**
     * 通过课程id获取课程详情
     * @param lessonId  课程id
     * @return
     */
    public LessonDetail getById(Integer lessonId) {
        if (lessonId == null) {
            log.error("[课程id为null]");
            return null;
        }
        return getLessonDetailBean(lessonId);
    }


    /**
     * 通过课程名称获取课程详情
     * @param lessonName  课程名
     * @return
     */
    public LessonDetail getByName(String lessonName) {
        if (StringUtils.isEmpty(lessonName)) {
            log.error("[课程名称为空]");
            return null;
        }
        Lesson lesson = lessonDao.get(null, lessonName);

        if (lesson == null) {
            log.error("[该课程不存在]");
            return null;
        }
        return getLessonDetailBean(lesson.getId());
    }

    /**
     * 添加课程详情
     * @param lessonDetail  LessonDetail实例
     */
    public void add(LessonDetail lessonDetail) {
        LessonTimeLine lessonTimeLine = lessonDetail.getLessonTimeLine();

        if (lessonTimeLine != null) {
            lessonDetailDao.addLessonTimeLine(lessonTimeLine);
        }
        LessonContent lessonContent = lessonDetail.getLessonContent();

        if (lessonContent != null) {
            lessonDetailDao.addLessonContent(lessonContent);
        }

    }



    /**
     * 通过课程id删除课程详情
     * @param lessonId  课程id
     */
    public void removeById(Integer lessonId) {
        lessonDetailDao.deleteLessonContent(lessonId);
        lessonDetailDao.deleteLessonTimeLine(lessonId, null);
    }

    /**
     * 更新课程详情
     * @param lessonDetail
     * @return
     */
    public int updateLessonDetail(LessonDetail lessonDetail) {
        LessonContent lessonContent = lessonDetail.getLessonContent();

        if (lessonContent != null && lessonContent.getLessonId() != null) {
            lessonDetailDao.updateLessonContent(lessonContent);
        }
        LessonTimeLine lessonTimeLine = lessonDetail.getLessonTimeLine();

        if (lessonTimeLine != null && lessonTimeLine.getId() != null) {
            return lessonDetailDao.updateLessonTimeLine(lessonTimeLine);
        }
        return MyReturnCode.UPDATE_LESSON_TIME_LINE_FAILED;
    }

    /**
     * 通过课程id获取某课程相关的详情总数
     * @param lessonId  课程id
     * @return  描述数量  +  课程时间数量
     */
    public int countLessonDetail(Integer lessonId) {

        return lessonDetailDao.countLessonContent(lessonId) + lessonDetailDao.countLessonTimeLine(lessonId);
    }

    /**
     * 通过课程id获取LessonDetail实例
     * @param lessonId  课程id
     * @return  返回LessonDetail实例
     */
    private LessonDetail getLessonDetailBean(Integer lessonId){
        LessonContent lessonContent = lessonDetailDao.getLessonContent(lessonId);
        LessonTimeLine lessonTimeLine = lessonDetailDao.getLessonTimeLine(lessonId);
        LessonDetail lessonDetail = new LessonDetail();
        if (lessonContent != null) {
            lessonDetail.setLessonContent(lessonContent);
        }
        if (lessonTimeLine != null) {
            lessonDetail.setLessonTimeLine(lessonTimeLine);
        }
        return lessonDetail;
    }


}
